
package com.be.beadmin.project.repository;


import com.be.beadmin.project.entity.BatchPlan;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author ljh
* @date 2023-02-05
**/
public interface BatchPlanRepository extends JpaRepository<BatchPlan, String>, JpaSpecificationExecutor<BatchPlan> {

    @Modifying
    @Transactional
    @Query(value = "update BatchPlan b set b.currentBatch = b.currentBatch-1 where b.masterPlanId = ?1 and b.currentBatch > ?2 and b.delete = 0" )
    void updateBatchPlanCurrentBatch(String masterPlanId, int currentBatch);


    int countBatchPlansByStateAndIdInAndDelete(int state, List<String> ids, Integer delete);

    BatchPlan findBatchPlanByIdAndDelete(String id, Integer delete);
    BatchPlan findBatchPlanById(String id);

//    @Query(value = "SELECT bp.master_plan_id from t_pm_batch_plan bp where bp.id = ?1 and bp.is_delete = ?2",nativeQuery = true)
//    BatchPlan findMasterPlanIdByIdAndDelete(String id, Integer delete);

    @Query("SELECT b.id from BatchPlan b where b.masterPlanId = ?1 and b.delete = ?2")
    List<String> findIdByMasterPlanIdAndDelete(String infoId, int delete);

    List<BatchPlan> findBatchPlanByMasterPlanIdAndStateNotAndDelete(String masterPlanId, int state, int deleted);

    @Modifying
    @Transactional
    @Query(value = "update t_pm_batch_plan bp set bp.is_delete = ?1 where bp.info_id = ?2 and bp.is_delete = ?3",nativeQuery = true)
    void deleteBatchPlanByInfoIdAndDelete(int deleted,String infoId,int nodelete);

    List<BatchPlan> findBatchPlanByMasterPlanIdAndDelete(String masterPlanId, int delete);


    @Query(value = "select bp.id from t_pm_batch_plan bp  where bp.master_plan_id = ?1 and bp.is_delete = ?2",nativeQuery = true)
    List<String> findBatchPlanIdByMasterPlanIdAndDelete(String masterPlanId, int delete);

    int countBatchPlanByMasterPlanIdAndDelete(String masterPlanId, int delete);

    BatchPlan findBatchPlanByCurrentBatchAndMasterPlanIdAndDelete(int currentBatch,String masterPlanId, int delete);

//    List<BatchPlan> findBatchPlanByMasterPlanIdAndBatchPlanIdAndDelete(String masterPlanId,String batchPlanIdNot, int delete);
    @Modifying
    @Transactional
    @Query(value = "update t_pm_batch_plan bp set bp.batch_count = ?1 where bp.master_plan_id = ?2 and bp.is_delete = ?3",nativeQuery = true)
    void updateBatchPlanCountByMasterPlanIdAndDelete(int count,String masterPlanId,int delete);


    @Transactional
    @Query(value = "select bp.batch_plan_code from t_pm_batch_plan bp",nativeQuery = true)
    List<String> findBatchPlanCodeByDelete(int delete);

    @Query(value = "select bp.is_schduling from t_pm_batch_plan bp where bp.id = ?1 and bp.is_delete = ?2",nativeQuery = true)
    int findIsSchduling(String id, int delete);

}